/**
 * Dashboard.Privacy.Custom
 * Extending the privacy interface, the custom object provides support for selecting users
 *
 * @author Seth
 * @version 0.2
 */
Dashboard.Privacy.Custom = function() {
	var initiated = false,
		error = false,
		store = {},
		users = [],

	// save function
	_save = function(z) {
		var d = $('#multi_users').val();

		// value is empty? throw error
		if (d == null) {
			Petolio.showMessage(_translate('custom_error'));
			return;
		}

		// save in post memory
		if(store.z) {
			store.z.savePerm({d: store.v, u: d});
		 	Dashboard.Privacy.changeState(store.x, store.y);
		 	z.dialog("close");
		 	return;
		}

		// save custom users
    	Petolio.ajax({
    		url: 'dashboard/privacy',
			type: 'post',
			data: {a: 'saveCustom', x: store.b, d: store.v, u: d},
			cache: false,
			success: function () {
				Dashboard.Privacy.changeState(store.x, store.y);
				z.dialog("close");
			}
		});

	// create the user window
	}, _createWindow = function(x) {
		// put the users in the selection
		var o = new String();
		$.each(users, function(s, i){
			var e = $.inArray(s, x) == -1 ? '' : ' selected="selected"';
			o += '<option value="'+ s +'"'+ e +'>'+ i.name +'</option>';
		});

		// this is the window
		return $('<div id="dialog-p_custom" title="' + _translate('custom_perms') + '" class="ui-state-highlight">'+
				'<div style="margin: 8px 0px 2px 0px; line-height: 18px;">'+
					_translate('custom_txt') +'<br /><div class="fivepx"></div><div class="fivepx"></div>'+
					'<select title="'+ _translate('custom_allowed') +'" name="users[]" class="chzn-select chzn-custom" multiple="multiple" style="width:275px;" id="multi_users">'+
						o+
					'</select><br /><br /><br /><br /><br /><br /><br /><br /><br />'+
				'</div>'+
			'</div>').dialog({
    			resizable: false,
    			modal: true,
    			autoOpen: false,
    			width: 300,
    			minHeight: 50,
    			buttons: [{
					text: _translate('save'),
					click: function() {
    					_save($(this));
    				}
				}, {
					text: _translate('cancel'),
					click: function() {
						$(this).dialog("close");
    				}
				}],
    			close: function() {
    				$("#dialog-p_custom").remove();
    			}
			});

	// load
	}, _load = function() {
		// error encountered? hold it right there mister
		if(error) {
			Petolio.showError();
			return false;
		}

		// memory load?
		if(store.z) {
			var w = _createWindow(store.z.store().perm.u);

			// open dialog and init chosen
			w.dialog('open');
			$("#multi_users").data('placeholder', $("#multi_users").attr('title')).chosen({translate: Petolio.translateChosen()});

			// skip the ajax below
			return;
		}

		// load custom permissions
		Petolio.showLoading();
    	Petolio.ajax({
    		url: 'dashboard/privacy',
			type: 'post',
			data: {a: 'loadCustom', x: store.b, d: store.v},
			cache: false,
			success: function (x) {
				Petolio.hideLoading();
				var w = _createWindow(x.users);

				// open dialog and init chosen
				w.dialog('open');
				$("#multi_users").data('placeholder', $("#multi_users").attr('title')).chosen({translate: Petolio.translateChosen()});
			}
		});

    // translate
	}, _translate = function(i) {
		return Dashboard.Translate.init(i);

	// constructor
	}, __construct = function(x, b, v, y, z) {
		// set store
		store = {x: x, b: b, v: v, y: y, z: z || false};

		// not initialized?
		if(!initiated) {
			Petolio.showLoading();
	    	Petolio.ajax({
	    		url: 'dashboard/privacy',
	    		type: 'post',
	    		data: {a: 'users'},
				cache: false,
				success: function (x) {
					// must load the users once
	    			Petolio.hideLoading();
					users = x.users;

					// load perms
					_load();
				},
				error: function() {
					Petolio.showError();
					error = true;
				}
			});

	    	// set as initialized
	    	initiated = true;
		} else _load();
	};

	// public
	return {
		init: __construct
	};
}();